package com.kele.message.response.write;

import com.kele.message.base.ResponseFrame;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

/**
 * 读取单个线圈或者寄存器的应答帧，即05,06功能码的应答帧
 */
public class WriteResponseFrame extends ResponseFrame {
    /**
     * 寄存器起始地址
     */
    private int address;
    /**
     * 寄存器内容
     */
    private ByteBuf content;

    public int getAddress() {
        return address;
    }

    public void setAddress(int address) {
        this.address = address;
    }

    public ByteBuf getContent() {
        return content;
    }

    public void setContent(ByteBuf content) {
        this.content = content;
    }

    @Override
    public void decode(ByteBuf in, int addressCode, int functionCode) {
        this.setAddressCode(addressCode);
        this.setFunctionCode(functionCode);
        this.setAddress(in.readShort());
        this.setContent(in.readRetainedSlice(2));
        this.setCheckCode(in.readRetainedSlice(2));
    }

    @Override
    public void release() {
        super.release();
        content.release();
    }

    @Override
    public String toString() {
        return "SingleWriteResponseFrame{" +
                "address=" + address +
                ", content=" + ByteBufUtil.hexDump(content) +
                "} " + super.toString();
    }
}
